package com.bootdo.front.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bootdo.front.vo.JobPositionTree;
import com.bootdo.front.vo.Result;
import com.bootdo.system.domain.IndustryDO;
import com.bootdo.system.vo.IndustryTree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.front.domain.JobPositionDO;
import com.bootdo.front.service.JobPositionService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 职位分类表
 * 
 * @author wjs
 * @email 1992lcg@163.com
 * @date 2020-02-21 14:17:35
 */
 
@Controller
@RequestMapping("/front/jobPosition")
public class JobPositionController {
	@Autowired
	private JobPositionService jobPositionService;
	
	@GetMapping()
	@RequiresPermissions("front:jobPosition:jobPosition")
	String JobPosition(){
	    return "front/jobPosition/jobPosition";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("front:jobPosition:jobPosition")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<JobPositionDO> jobPositionList = jobPositionService.list(query);
		int total = jobPositionService.count(query);
		PageUtils pageUtils = new PageUtils(jobPositionList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("front:jobPosition:add")
	String add(){
	    return "front/jobPosition/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("front:jobPosition:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		JobPositionDO jobPosition = jobPositionService.get(id);
		model.addAttribute("jobPosition", jobPosition);
	    return "front/jobPosition/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("front:jobPosition:add")
	public R save( JobPositionDO jobPosition){
		if(jobPositionService.save(jobPosition)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("front:jobPosition:edit")
	public R update( JobPositionDO jobPosition){
		jobPositionService.update(jobPosition);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("front:jobPosition:remove")
	public R remove( Integer id){
		if(jobPositionService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("front:jobPosition:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		jobPositionService.batchRemove(ids);
		return R.ok();
	}


	@GetMapping( "/getAll")
	@ResponseBody
	public Result getAll(){
		List<JobPositionTree> trees = new ArrayList<>();

		List<JobPositionDO> list = jobPositionService.list(null);
		if(list != null && !list.isEmpty()){
			List<JobPositionDO> collects = list.stream().filter(p -> Integer.compare(0, p.getParentNo()) == 0).collect(Collectors.toList());
			if(collects != null && !collects.isEmpty()){
				for(JobPositionDO jobPosition : collects){
					JobPositionTree tree = new JobPositionTree();
					trees.add(tree);
					tree.setPositionNo(jobPosition.getPositionNo());
					tree.setPositionName(jobPosition.getPositionName());
					tree.setId(jobPosition.getId());
					List<JobPositionDO> collectChildren = list.stream().filter(p -> Integer.compare(jobPosition.getPositionNo(), p.getParentNo()) == 0).collect(Collectors.toList());

					List<JobPositionTree> nodes = new ArrayList<>();
					tree.setNodes(nodes);
					if(collectChildren != null && !collectChildren.isEmpty()){
						for(JobPositionDO jobPositionDO : collectChildren) {
							JobPositionTree treec = new JobPositionTree();
							treec.setPositionNo(jobPositionDO.getPositionNo());
							treec.setPositionName(jobPositionDO.getPositionName());
							treec.setId(jobPositionDO.getId());
							nodes.add(treec);
						}
					}
				}
			}
		}

		return 	Result.ok(trees);
	}
}
